/*
 * File     : Function.java
 * Created  : 1 May 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.library;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.googlecode.dni.DirectNativeInterface;
import com.googlecode.dni.type.structure.Structure;
import com.googlecode.dni.type.structure.StructureOptions;


/**
 * <p>
 *  Annotates a method of a native interface that maps to a native function.
 * </p>
 * <h3>Use on {@link Library} interfaces}
 * <p>
 *  When the annotated method is on a {@link Library} interface, it provides
 *  details of the native function.  For example, the name of the function can
 *  be specified.
 * </p>
 * <h3>Use on {@link Structure} interfaces}
 * <p>
 *  When the annotated method is on a {@link Structure}, it indicates that it is
 *  a method that operates on the structure.  The method will map to a function
 *  that takes the method's arguments with the structure type (as a pointer)
 *  prepended; for example, a method on a type <code>MyStruct</code>:
 * </p>
 * <pre>
 *  int computeAnswer( float x, float y );
 * </pre>
 * <p>
 *  would map to:
 * </p>
 * <pre>
 *  int computeAnswer( MyStruct struct, float x, float y );
 * </pre>
 * <p>
 *  on some library type.
 * </p>
 * <p>
 *  To identify the library, the library is found in this order (decreasing
 *  priority):
 * </p>
 * <ol>
 *  <!-- TODO v1.1 this could be extended -->
 *  <li>
 *   the {@link Structure}'s {@link StructureOptions#library() library}
 *   annotation;
 *  </li>
 *  <li>
 *   the {@link Structure}'s {@link StructureOptions#libraryType() libraryType},
 *   where that type is annotated with {@link Library}.
 *  </li>
 * </ol>
 *
 * @author Matthew Wilson
 */
@Retention( RetentionPolicy.RUNTIME )
@Target( ElementType.METHOD )
@Documented
public @interface Function
{

    /**
     * Special pseudo-character-set name for the default library character set.
     */
    String LIBRARY_CHARSET = "##library##";

    /**
     * <p>
     *  The native name of the function.
     * </p>
     * <p>
     *  The default name (if this property is not set) will be taken from the
     *  method name.  Name "mangling" by the calling convention is done after
     *  the base name is determined.
     * </p>
     * <p>
     *  The {@linkplain Library#functionPrefix() library's function name prefix}
     *  is not applied to this name.
     * </p>
     *
     * @return the name, or an empty string to use the default name
     */
    String name() default "";

    /**
     * <p>
     *  Whether to mangle the name of the function.
     * </p>
     * <p>
     *  By default, the calling convention is allowed to "mangle" the name of the
     *  function.  If this property is set to <code>false</code>, that mangling
     *  does not occur; hence, you will need to set the mangled name in the
     *  {@link #name()} property.
     * </p>
     *
     * @return whether to mangle the name
     */
    boolean mangleName() default true;

    /**
     * <p>
     *  The name of the character set to use.
     * </p>
     * <p>
     *  By default, the platform character set is chosen.
     * </p>
     *
     * @return the character set name
     */
    String charset() default LIBRARY_CHARSET;

    /**
     * <p>
     *  Whether the function is optional.
     * </p>
     * <p>
     *  If the function is not present at run-time in the dynamic library, an
     *  info message is logged, and the Java method implementation will throw
     *  {@link UnsupportedOperationException}.
     * </p>
     *
     * @return whether the function is optional
     */
    boolean optional() default false;

    /**
     * <p>
     *  Whether to read the platform's 'last error' value after the call.
     * </p>
     * <p>
     *  The 'last error' mechanism is <code>GetLastError</code> under Windows, and
     *  <code>errno</code> under POSIX.
     *  The value can be read by calling
     *  {@link DirectNativeInterface#getLastError()} on the same thread.
     *  There is a small overhead of adding this check into the native call.
     * </p>
     * <p>
     *  (Note that a mandated exception was not chosen as the design since some
     *  functions return values that can be processed <em>and</em> set an error
     *  code; other calls only modify the value on failure cases.
     *  Furthermore, it is not possible to make the same system call to read the
     *  value since JNI and other Java internals may well corrupt the value
     *  between the two calls.)
     * </p>
     *
     * @return the behaviour for reading 'last error'
     */
    LastErrorBehaviour lastError() default LastErrorBehaviour.IGNORE;

    /**
     * <p>
     *  The behaviour of a function with regards to the platform's 'last error'
     *  value.
     * </p>
     *
     * @author Matthew Wilson
     *
     * @see Function#lastError()
     */
    public enum LastErrorBehaviour
    {
        /**
         * The call will not read the last error value.
         */
        IGNORE,

        /**
         * The call will read and record the last error value.  It will be
         * accessible from {@link DirectNativeInterface#getLastError()}.
         */
        SET,

        /**
         * The call will read and record the last error value, and throw if the
         * value is non-zero.  The value will also be accessible from
         * {@link DirectNativeInterface#getLastError()}.
         */
        THROW,
    }

}
